package test.test02.MyThread;

class ThreadState{
    public static void main(String[] args) {
        // for(Thread.State state:Thread.State.values()){
        //     System.out.println(state);
        // }

        // Thread thread = new Thread();
        // System.out.println(thread.getState());//NEW

        // Thread thread = new Thread(()->{
        //     System.out.println(Thread.currentThread().getState());//RUNNABLE
        // });
        // thread.start();

        Object locker = new Object();
        Thread t1 = new Thread(()->{
            try {
                System.out.println("t1进入TIMED_WAITING");
                Thread.sleep(1000);
                System.out.println("t1结束TIMED_WAITING");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized(locker){
                try {
                    System.out.println("t1进入WAITING");
                    locker.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        try {
            for (int i = 0; i < 2; i++) {
                Thread.sleep(500); // 每0.5秒检查一次
                System.out.println("t1线程状态: " + t1.getState());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread t2 = new Thread(()->{
        synchronized(locker){
            locker.notify();
            System.out.println("t1并不会立马被唤醒，需要等t2释放锁");
            System.out.println("t1线程状态: " + t1.getState());//BLOCKED
        }
        });
        t2.start();
        try {
        t2.join();
        System.out.println("t2唤醒t1后");
        System.out.println("t1线程状态: " + t1.getState());//TERMINATED
        } catch (InterruptedException e) {
        e.printStackTrace();
        }

        // Thread thread = new Thread();
        // System.out.println(thread.getState());//NEW
        // thread.start();
        // System.out.println(thread.getState());//RUNNABLE
        // try{
        //     Thread.sleep(1000);
        // }catch(InterruptedException e){
        //     e.printStackTrace();
        // }
        // System.out.println(thread.getState());//TERMINATED

        // Thread sleepThread = new Thread(() -> {
        //     try {
        //         System.out.println("线程开始执行，准备进入睡眠状态");
        //         Thread.sleep(1000); // 睡眠1秒，会进入TIMED_WAITING状态
        //         System.out.println("线程睡眠结束");
        //     } catch (InterruptedException e) {
        //         e.printStackTrace();
        //     }
        // });

        // // 启动线程
        // sleepThread.start();

        // // 主线程定期检查sleepThread的状态
        // try {
        //     for (int i = 0; i < 2; i++) {
        //         Thread.sleep(500); // 每0.5秒检查一次
        //         System.out.println("线程状态: " + sleepThread.getState());
        //     }
        // } catch (InterruptedException e) {
        //     e.printStackTrace();
        // }
    }
}
